package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.ChangeRecord;
import com.fx.entity.UserInfo;
import com.fx.entity.WalletInfo;
import com.fx.service.ChangeRecordService;
import com.fx.service.UserInfoService;
import com.fx.service.WalletInfoService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/walletInfo")
public class WalletInfoController extends BaseController {
    @Autowired
    private WalletInfoService walletInfoService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ChangeRecordService changeRecordService;
    private static String pageTitle = "用户钱包管理";
    private static String pageUrl = "/walletInfo";

    @GetMapping()
    String walletInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "walletInfo/walletInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<WalletInfo> walletInfoList = walletInfoService.getList(query);
        int total = walletInfoService.getCount(query);
        return renderPage(total, walletInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "walletInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        WalletInfo walletInfo = walletInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("walletInfo", walletInfo);
        return "walletInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(WalletInfo walletInfo) {
        String id = walletInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = walletInfoService.save(walletInfo);
        } else {
            result = walletInfoService.updateById(walletInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (walletInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 方法名称：我的佣金接口
     * 概要说明：我的佣金接口
     *
     * @Ather EDZ
     * @Date 2018-6-25 15:00
     * @Param userId 用户id
     * @Return
     */
    @GetMapping("/api/userBrokerage/{userId}")
    @ResponseBody
    public R userBrokerage(@PathVariable("userId") String userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        Map<String, Object> result = new HashMap<String, Object>();
        WalletInfo walletInfo = walletInfoService.getById(userInfo.getFlWallet());
        List<ChangeRecord> changeRecordList = changeRecordService.getListByUserId(userId);
        result.put("wallet", walletInfo);
        result.put("changeRecord", changeRecordList);
        return R.ok(result);
    }

    /**
     * 方法名称：提现接口
     * 概要说明：用户提现
     * @Ather EDZ
     * @Date 2018-6-25 15:29
     * @Param userId 用户Id
     * @Param withdrawAmount 提现金额
     * @Param realName 真实姓名
     * @Return
     */
    @PostMapping("/api/withdraw")
    @ResponseBody
    public R withdraw(HttpServletRequest request) throws WxPayException {
        String userId = String.valueOf(request.getAttribute(Constant.CLIENT_USER_ID)) == null ? request.getParameter("userId") : String.valueOf(request.getAttribute(Constant.CLIENT_USER_ID));
        Double withdrawAmount = Double.valueOf(request.getParameter("withdrawAmount"));
        String realName = request.getParameter("realName");
        String clientIp = request.getRemoteAddr();
        Integer status = walletInfoService.withdrawByUserId(userId, withdrawAmount, realName, clientIp);
        if ( status > 0) {
            return R.ok("提现成功");
        }
        if(status == -1){
            return R.ok("提现金额不能超过可提现金额");
        }
        return R.error("提现失败");
    }

    @GetMapping("/api/getCommissionInfo/{userId}")
    @ResponseBody
    public R getCommissionInfo(@PathVariable("userId") String userId){
       WalletInfo walletInfo = walletInfoService.getCommissionInfo(userId);
       return R.ok(walletInfo);
    }

}
